﻿using GMall.AdministrationService.Menus;
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;
using Volo.Abp.Identity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.PermissionManagement;

namespace GMall.AdministrationService.DataSeed
{
    public class PermissionDataSeeder : Volo.Abp.PermissionManagement.PermissionDataSeeder
    {
        private readonly IRepository<PermissionGrant, Guid> _permissionRepository;
        private readonly IRepository<Menu, Guid> _menuRepository;
        private readonly IMenuDataSeeder _menuDataSeeder;
        private readonly IdentityRoleManager _roleManager;
        public PermissionDataSeeder(IPermissionGrantRepository permissionGrantRepository, IGuidGenerator guidGenerator, ICurrentTenant currentTenant,
            IRepository<Menu, Guid> menuRepository,
            IMenuDataSeeder menuDataSeeder,
            IdentityRoleManager roleManager,
            IRepository<PermissionGrant, Guid> permissionRepository
            ) : base(permissionGrantRepository, guidGenerator, currentTenant)
        {
            _menuRepository = menuRepository;
            _menuDataSeeder = menuDataSeeder;
            _roleManager = roleManager;
            _permissionRepository = permissionRepository;
        }
        public override async Task SeedAsync(
        string providerName,
        string providerKey,
        IEnumerable<string> grantedPermissions,
        Guid? tenantId = null)
        {
            using (CurrentTenant.Change(tenantId))
            {
                // 先执行菜单种子数据
                await _menuDataSeeder.SeedAsync(CurrentTenant.Id);
                // 查找管理员账号
                var role = await _roleManager.FindByNameAsync("admin");
                providerKey = role.Id.ToString();
                var permissionNames = (await _menuRepository.GetQueryableAsync())
                    .Where(m => m.IsActive)
                    .Select(m => m.Id.ToString())
                    .ToArray();

                var names = permissionNames.ToArray();// grantedPermissions.ToArray();

                var existsPermissionGrants = (await _permissionRepository.GetListAsync(p => p.ProviderKey == providerKey && p.ProviderName == providerName)).Select(x => x.Name).ToList();

                foreach (var permissionName in names.Except(existsPermissionGrants))
                {
                    await PermissionGrantRepository.InsertAsync(
                        new PermissionGrant(
                            GuidGenerator.Create(),
                            permissionName,
                            providerName,
                            providerKey,
                            tenantId
                        )
                    );
                }
            }
        }
    }

    //public class PermissionDataSeedContributor : Volo.Abp.PermissionManagement.PermissionDataSeedContributor
    //{
    //    private readonly IRepository<Menu, Guid> _menuRepository;
    //    public PermissionDataSeedContributor(IPermissionDefinitionManager permissionDefinitionManager, IPermissionDataSeeder permissionDataSeeder, ICurrentTenant currentTenant, IRepository<Menu, Guid> menuRepository) : base(permissionDefinitionManager, permissionDataSeeder, currentTenant)
    //    {
    //        _menuRepository = menuRepository;
    //    }
    //    public override async Task SeedAsync(DataSeedContext context)
    //    {
    //        var multiTenancySide = CurrentTenant.GetMultiTenancySide();
    //        //var permissionNames = PermissionDefinitionManager
    //        //    .GetPermissions()
    //        //    .Where(p => p.MultiTenancySide.HasFlag(multiTenancySide))
    //        //    .Where(p => !p.Providers.Any() || p.Providers.Contains(RolePermissionValueProvider.ProviderName))
    //        //    .Select(p => p.Name)
    //        //    .ToArray();

    //        var permissionNames = (await _menuRepository.GetQueryableAsync())
    //            .Where(m => m.IsActive)
    //            .Select(m => m.Code)
    //            .ToArray();

    //        await PermissionDataSeeder.SeedAsync(
    //            RolePermissionValueProvider.ProviderName,
    //            "admin",
    //            permissionNames,
    //            context?.TenantId
    //        );
    //    }
    //}
}
